layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index', 'form', 'jquery', 'table'], function () {
    var form = layui.form,
        table = layui.table,
        laypage = layui.laypage,
        admin = layui.admin;

    table.render({
        elem: '#ad_list_table'
        , url: '/advert/queryAdvertBatchlList'
        , method: "post"
        , height: 500
        , limit: 50							//显示数目
        , limits: [50, 100]
        , cols: [[
            {field: 'batchName', title: '批次名', width: 200, "align": "center"}
            , {
                field: 'status', width: 200, title: '状态', "align": "center", templet: function (d) {
                    var type = d.status;
                    if (type == 0) {
                        return "未配置广告";
                    } else if (type == 1) {
                        return "未配置设备";
                    } else if (type == 2) {
                        return "可推送";
                    }else if(type == 3){
                        return "推送成功";
                    }else if(type == -1){
                        return "推送失败";
                    }
                }
            }
            , {
                field: 'count', width: 200, title: '本次推送设备数量', "align": "center", templet: function (d) {
                    if(d.count==null){
                        return 0;
                    }else {
                        return d.count;
                    }
                },
                sort: true
            }

            , {field: 'createdTime', width: 200, title: '创建时间', "align": "center" ,sort: true}
            , {field: 'OPT', width: 200, title: '操作', "align": "left", toolbar: "#OptData"}
        ]]
        , request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where: {
            status: '',
            batchName:''
        }
        , id: 'ad_list_table'
        , page: true
    });

    $(".search_btn").click(function () {
        var status = $(".queryTable select[name='status']").val();
        var batchName = $(".queryTable input[name='batchName']").val();
        table.reload('ad_list_table', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                status: status,
                batchName: batchName
            }
        });
    });

    table.on('tool(ad_list_table)', function(obj){
        var table_event = obj.event;
        var data = obj.data;
        var trId = obj.data.id;
        if (table_event=="edit_ad"){
            // 弹出层
            admin.popup({
                title: '配置广告'
                ,shade: 0
                ,anim: -1
                ,offset:"20px"
                ,area: ['80%', '750px']
                ,skin: 'layui-anim layui-anim-upbit'
                ,content:$("#edit_ad")
                ,end:function () {
                    //   详情结束 清除掉内容
                }
            });

        }else if (table_event=="edit_ter"){
            // 弹出层
            admin.popup({
                title: '配置设备'
                ,shade: 0
                ,anim: -1
                ,offset:"20px"
                ,area: ['80%', '600px']
                ,skin: 'layui-anim layui-anim-upbit'
                ,content:$("#edit_ter")
                ,end:function () {
                    //   详情结束 清除内容
                    $('#add_task_push_table').empty();
                }
            });
            // 设备表格数据
            table.render({
                elem: '#add_task_push_table'
                ,id: 'add_task_push_table'
                ,height: 400
                ,url: '/advert/queryTerminalList' //数据接口
                ,method:"post"
                ,page: true //开启分页
                ,limit:50							//显示数目
                ,limits:[50,100]
                ,cols: [[ //表头
                    {type: 'checkbox'}
                    ,{field: 'id', title: 'ID', width:80,"align":"center"}
                    ,{field: 'storeName', title: '店名', width:200,"align":"center"}
                    ,{field: 'type', title: '设备类型', width:180,"align":"center"}
                    ,{field: 'deviceId', title: '设备号', width:180,"align":"center"}
                ]]
            });


        }else if(table_event=="info"){
            // 弹出层
            admin.popup({
                title: '广告详情'
                ,shade: 0
                ,anim: -1
                ,offset:"20px"
                ,area: ['80%', '700px']
                ,skin: 'layui-anim layui-anim-upbit'
                ,content:$("#info_div")
                ,end:function () {
                    //   详情结束 清除掉广告div的内容
                    $('#img_display').empty();
                }
            });
            //详情开始 发ajax 去渲染内的广告
            $.ajax({
                type: "POST",
                url: "/advert/queryBatchlinfo",
                data: {
                    "batchId": data.id
                },
                success: function (res) {
                    var resData = res.data;
                    if (res.code == 1) {
                        if (res.data.length=="0"){
                            $('#img_display').empty();
                            $('#img_display').append('<span>'+"暂无查询类容"+'</span>');
                            $('#pageContent').hide();
                        }else {
                            $('#pageContent').show();
                            $('#img_display').empty();
                            layui.each(resData, function(index){
                                if(resData[index].ad_type=="image"){
                                    $('#img_display').append('<div class="fileBox">'
                                        +'<div>'+'<img src="'+ resData[index].url +'" class="layui-upload-img">'
                                        +'</div>'+'<div><span>'+'<b>标题:</b>'+resData[index].ad_title
                                        +'</span><span>'+'<b>时间:</b>'+resData[index].updatedTime+'</span></div>'
                                        +'</div>');
                                }else if(resData[index].ad_type=="video"){
                                    $('#img_display').append('<div class="fileBox">'
                                        +'<div>'+'<video controls src="'+ resData[index].url
                                        +'" class="layui-upload-img play_video">'+'</video>'+'<span class="v_delete"></span>'
                                        +'<div class="video_bg"><img src="/images/bofang.png"/></div>'+'</div>'+'<div><span>'
                                        +'<b>标题:</b>'+resData[index].ad_title+'</span><span>'+'<b>时间:</b>'
                                        +resData[index].updatedTime+'</span></div>'
                                        +'</div>')
                                }
                            });
                        }


                    } else {
                        layer.msg(res.message);
                    }
                }
            });
        }else if(table_event=="push"){
            //推送发ajax 去后台开始推送
            $.ajax({
                type: "POST",
                url: "/advert/adPush",
                traditional: true,
                data: {
                    "batchId": trId
                },
                success: function (res) {
                    if (res.code == 1) {
                        layer.alert('推送成功',{icon: 6}, function(index){
                            location.reload();
                        });
                    } else {
                        layer.msg(res.message);
                    }
                }
            });
        }



        //配置广告开始, 获取分页总数
        $.ajax({
            type: "POST",
            url: "/advert/queryAdList",
            success: function (res) {
                var count = res.count;
                if (res.code == 1) {
                    pageupImgFn(count);
                } else {
                    layer.msg(res.message);
                }
            }
        });

        // 查询开始
        $('.search_btn_img').click(function () {
            var checkType = $('.checkType option:selected').val();
            var queryVal = $('.queryImg input').val();
            $.ajax({
                type: "POST",
                url: "/advert/queryAdList",
                data: {
                    "ad_type": checkType,
                    "ad_title": queryVal
                },
                success: function (res) {
                    var queryCount = res.count;
                    if (res.code == 1) {
                        pageupImgFn(queryCount,queryVal,checkType)
                    } else {
                        layer.msg(res.message);
                    }
                }
            });
        });
        // 查询结束

        // 文件分页显示方法
        function pageupImgFn(count,ad_title,ad_type) {
            laypage.render({
                elem: 'pageContent'
                ,count: count
                ,layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
                ,jump: function(obj){
                    var pageSize = obj.limit;
                    var pageNo = obj.curr;
                    $.ajax({
                        type: "POST",
                        url: "/advert/queryAdList",
                        data: {
                            "ad_type": ad_type,
                            "ad_title": ad_title,
                            "pageSize": pageSize,
                            "pageNo": pageNo
                        },
                        success: function (res) {
                            var resData = res.data;
                            if (res.code == 1) {
                                if (res.data.length=="0"){
                                    $('#edit_display').empty();
                                    $('#edit_display').append('<span>'+"暂无查询类容"+'</span>');
                                    $('#pageContent').hide();
                                }else {
                                    $('#pageContent').show();
                                    $('#edit_display').empty();
                                    layui.each(resData, function(index){
                                        if(resData[index].ad_type=="image"){
                                            $('#edit_display').append('<div class="fileBox">'
                                                +'<div>'+'<img src="'+ resData[index].url +'" class="layui-upload-img">'
                                                +'</div>'+'<div><span>'+'<b>标题:</b>'+resData[index].ad_title
                                                +'</span><span>'+'<b>时间:</b>'+resData[index].updatedTime+'</span></div>'
                                                +'<div class="checkbox_input">'
                                                +'<input type="checkbox" name="fileType" value="'+resData[index].id+'">'
                                                +'选择推送'+'</div>'
                                                +'</div>');
                                        }else if(resData[index].ad_type=="video"){
                                            $('#edit_display').append('<div class="fileBox">'
                                                +'<div>'+'<video controls src="'+ resData[index].url
                                                +'" class="layui-upload-img play_video">'+'</video>'+'<span class="v_delete"></span>'
                                                +'<div class="video_bg"><img src="/images/bofang.png"/></div>'+'</div>'+'<div><span>'
                                                +'<b>标题:</b>'+resData[index].ad_title+'</span><span>'+'<b>时间:</b>'
                                                +resData[index].updatedTime+'</span></div>'
                                                +'<div class="checkbox_input">'
                                                +'<input type="checkbox" name="fileType" value="'+resData[index].id+'">'
                                                +'选择推送'+'</div>'
                                                +'</div>')
                                        }
                                    });
                                }


                            } else {
                                layer.msg(res.message);
                            }
                        }
                    });

                }
            });
        }

        // 保存配置广告开始
        $('.ad_placement').on('click',function () {
            var push_file = $('#edit_display .fileBox input:checked');
            var fileArr = [];
            for(var i=0;i<push_file.length;i++){
                var fileArrVal = Number(push_file[i].value);
                fileArr.push(fileArrVal);
            }
            $.ajax({
                type: "POST",
                url: "/advert/updatAd",
                traditional: true,
                data: {
                    "adList": fileArr.toString(),
                    "id": trId
                },
                success: function (res) {
                    if (res.code == 1) {
                        layer.alert('保存成功',{icon: 6}, function(index){
                            location.reload();
                            layer.closeAll();
                        });
                    } else {
                        layer.msg(res.message);
                    }
                }
            });

        });
        // 保存配置广告结束

        // 点击全选开始
        var onOff = 0;
        $('.checkbox_all_but').on('click',function () {
            if (onOff===0){
                $(this).css({'background':'#5FB878','border-color':'#5FB878','color':'#fff'});
                onOff = 1;
            }else {
                $(this).css({'background':'none','border-color':'#d2d2d2','color':'#f2f2f2'});
                onOff = 0;
            }
        });
        // 点击全选结束

        // 保存设备开始
        $('.ad_equipment').on('click',function () {
            var checkStatus = table.checkStatus('add_task_push_table'); //获取对应的表格
            var checkStatusData = checkStatus.data;//获取选中行的数据
            // var checkStatusIsAll = checkStatus.isAll;//表格是否全选
            var checkData = [];//获取选中行数据的id
            for(var j=0;j<checkStatusData.length;j++){
                var checkDataId = Number(checkStatusData[j].id);
                checkData.push(checkDataId);
            }
            var allPush;
            if(onOff===1){//判断是否全选
                allPush = 1;
                checkData = '';
            }else {
                allPush = 0;
            }
            $.ajax({
                type: "POST",
                url: "/advert/updatTerminal",
                traditional: true,
                data: {
                    "terList": checkData.toString(),
                    "id": trId,
                    "allPush": allPush
                },
                success: function (res) {
                    if (res.code == 1) {
                        layer.alert('保存成功',{icon: 6}, function(index){
                            location.reload();
                            layer.closeAll();
                        });
                    } else {
                        layer.msg(res.message);
                    }
                }
            });
        });
        // 保存设备结束


    });

    // 点击播放视频开始
    $(document).on("click",".video_bg",function(){
        var _this = $(this);
        var video = _this.siblings('video');//视频
        var span = _this.siblings('span');//关闭视频按钮
        var videoBox = _this.parent();//装视频盒子
        video.trigger('play');
        _this.hide();
        videoBox.addClass('enlarge');
        span.click(function () {
            videoBox.removeClass('enlarge');
            video.trigger('pause');
            _this.show();
        });

    });
    // 点击播放视频结束

    // 创建推送任务弹出层开始
    $('.task_push').click(function () {
        admin.popup({
            title: '创建推送任务'
            ,shade: 0
            ,anim: -1
            ,area: ['60%', '170px']
            ,content:$("#add_task_push")
            ,end:function () {
            }
        });
    });
    // 创建推送任务弹出层结束

    // 保存推送任务
    $('.preservation_push').click(function () {
        var advertBatch = $('.preservation_box input').val();
        if(advertBatch){
            $.ajax({
                type: "POST",
                url: "/advert/insertAdvertBatch",
                traditional: true,
                data: {
                    "batchName": advertBatch
                },
                success: function (res) {
                    if (res.code == 1) {
                        $('.preservation_box input').val('');
                        layer.alert('保存成功',{icon: 6}, function(index){
                            location.reload();
                            layer.closeAll();
                        });
                    } else {
                        layer.msg(res.message);
                    }
                }
            });
        }else {
            layer.msg('请输入批次');
        }
    });
    // 保存推送任务结束

});